/*
 *@本JS功能：班级管理
 *@本js作者：杨锡君
 *@编写时间：2017年10月31日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $departNames = $("#departNames");
    var $tableBox = $(".tableBox");
    var $searchBtn = $(".searchBtn");
    var $exportEvent = $(".exportBtn");
    var $exportCancle = $(".exportCancle");
    var $fileName = $(".fileName");
    var th = "<tr>" +
        "<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-2\">院系</th>" +
        "<th class=\"col-lg-2\">专业</th>" +
        "<th class=\"col-lg-2\">班级</th>" +
        "<th class=\"col-lg-2\">学生数量</th>" +
        "<th class=\"col-lg-3\">操作</th>" +
        "</tr>";

    function tabelModel(opt) {
        return "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\"><input data-classid=" + opt.classId + " value=\"0\" name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" /></td>" +
            "<td class=\"col-lg-2\" title=" + opt.departmentName + ">" + opt.departmentName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.professionName + ">" + opt.professionName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.className + ">" + opt.className + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.stuCount + ">" + opt.stuCount + "人</td>" +
            "<td class=\"col-lg-3\">" +
            "<a href=\"javascript:;\" data-classid=" + opt.classId + "  class=\"editBtn\">修改</a>" +
            "<a href=\"javascript:;\" data-profess='"+ opt.professionName +"' data-depart='"+ opt.departmentName +"' data-classname="+ opt.className +" data-classid=" + opt.classId + "  class=\"detailBtn\">学生管理</a>" +
            "</td>" +
            "</tr>";
    }
    var classMana = {
        some:'',
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            var json = {
                "pageNow": 1,
                "pageSize": 10
            };

            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    json = result.paramData.some;
                    if(json.departmentId!=undefined){
                        $departNames.val(json.departmentId);
                    }
                }
            }
            self.some = json;
            self.tabelList(json);
            $(".delClass").off("click").on("click", function() {
                self.delListEvent();
            });
            $searchBtn.off("click").on("click", function() {
                var departmentId = $departNames.val();
                var departmentName = $departNames.children("option:selected").html();
                var jsonStr = {
                    "pageNow": 1,
                    "pageSize": 10
                };
                if(departmentId == 0) {
                    departmentName = "";
                } else {
                    jsonStr.departmentId = departmentId;
                    jsonStr.departmentName = departmentName;
                }
                self.tabelList(jsonStr);
                self.some = jsonStr;
            });
            //多班级导入
            self.muchExportStuEvent();


        },
        muchExportStuEvent:function(){
            //多班级导入
            var self =this;
            var classId;
            $(".muchImport").off("click").click(function(){
                $(".popup,.mb").show();
            });

            var flag = self.fileEvent();
            $exportCancle.off("click").click(function() {
                $("#file").val("");
                $(".popup,.mb").hide();
                $fileName.html("");
            });
            $(".popup_img").off("click").click(function() {
                $("#file").val("");
                $(".popup,.mb").hide();
                $fileName.html("");
            });
            //	批量导入

            $("#uploadForm").off("submit").submit(function(event){
                var formData = new FormData(this);

                if($fileName.html() != ""){
                    confirm1.content = "是否确认上传文件？";
                    confirm1.success = function() {
                        loading1.show();
                        setTimeout(function (args) {
                            $.ajax({
                                url: 'imStuExcelByManyClass',
                                type: 'post',
                                data: formData,
                                async: false,
                                cache: false,
                                contentType: false,
                                processData: false,
                                success: function (msg) {
                                    loading1.hide();
                                    msg = JSON.parse(msg);
                                    if(msg.result == "success"){
                                        Alert1.content = "导入成功";
                                        Alert1.success = function () {
                                            var json = JSON.stringify({
                                                "pageName":"adminClassMana"
                                            });
                                            window.location.href = "skipPage?jsonStr="+json;
                                        };
                                        Alert1.init();
                                        Alert1.show();
                                    }else{
                                        $("#file").val("");
                                        $fileName.html("");
                                        Alert1.content = msg.errMsg;
                                        Alert1.init();
                                        Alert1.show();
                                        return false;

                                    }
                                    return false;
                                },
                                error:function () {
                                    var json = JSON.stringify({
                                        "pageName": "error"
                                    });
                                    window.location.href = "skipPage?jsonStr=" + json;
                                }
                            });
                            return false;
                        },1000);
                        return false;
                    };
                    confirm1.init();
                    confirm1.show();

                }else{
                    Alert1.content = "请上传Excel文件，谢谢合作！";
                    Alert1.init();
                    Alert1.show();
                }
                return false;
            });


        },
        fileEvent: function() {
            $("#file").off("change").change(function() {
                if($("#file").val()!=""){
                    var type = $(this).val().substring($(this).val().lastIndexOf(".")).toLowerCase(); // 取文件格式后缀名
                    if(type != ".xls" && type != ".xlsx") {
                        $("#file").val("");
                        Alert1.content = "请上传Excel文件，谢谢合作！";
                        Alert1.init();
                        Alert1.show();
                        return false;
                    } else {
                        var filename = $(this).val().substring($(this).val().lastIndexOf("\\")+1);
                        $fileName.html("已选:"+filename);
                        return true;
                    }
                }else{
                    $("#file").val("");
                    $fileName.html("");
                }

            });
        },
        tabelList: function(jsons) {
            var self = this;
            var json = JSON.stringify(jsons);
            loading1.show();
            $.ajax({
                type: "post",
                url: "getClassList",
                data: {
                    "jsonStr": json
                },
                async: true,
                success: function(msg) {
                    loading1.hide();
                    msg = JSON.parse(msg);
                    $tableBox.empty();
                    self.skipEvent();
                    if(msg.result == "success"){
                        if(msg.classList.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.classList, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $tableBox.append(th + tabel);
                            self.allCheckEvent();
                            self.skipEvent();

                        } else {
                            var trImg = "<tr><td colspan='6'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = msg.pageNow;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "getClassList",
                                    data: {
                                        "jsonStr": JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        //获取院系
                                        $departNames.html("<option value=\"0\">请选择</option>");
                                        $.map(msg.departMentList, function(opt) {
                                            $departNames.append("<option value=" + opt.departmentId + ">" + opt.departmentName + "</option>");
                                        });
                                        if(msg.SearObjectMap.departmentId!=undefined){
                                            $departNames.val(msg.SearObjectMap.departmentId);
                                        }
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.classList, function(opt) {
                                            tabel += tabelModel(opt);
                                        });
                                        $tableBox.append(th + tabel);
                                        self.allCheckEvent();
                                        self.skipEvent();
                                    }
                                });
                            }
                        });
                        //获取院系
                        $departNames.html("<option value=\"0\">请选择</option>");
                        $.map(msg.departMentList, function(opt) {
                            $departNames.append("<option value=" + opt.departmentId + ">" + opt.departmentName + "</option>");
                        });
                        if(msg.SearObjectMap.departmentId!=undefined){
                            $departNames.val(msg.SearObjectMap.departmentId);
                        }
                    }else{
                        if(msg.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }

                },
                error:function () {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;
                }
            });

        },
        skipEvent:function(){
            var self = this;
//			详情
            $(".detailBtn").click(function(){
                var json = JSON.stringify({
                    "pageName":"adminClassDetail",
                    "paramData":{
                        "classId":Number($(this).attr("data-classid")),
                        "className":$(this).attr("data-classname"),
                        "depart":$(this).attr("data-depart"),
                        "profess":$(this).attr("data-profess"),
                        "some":self.some
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
//			编辑班级
            $(".editBtn").click(function(){
                var json = JSON.stringify({
                    "pageName":"adminClassEdit",
                    "paramData":{
                        "classId":$(this).attr("data-classid")
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
//			添加班级
            $(".addClass").click(function(){
                var json = JSON.stringify({
                    "pageName":"adminClassAdd"
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
        },
        delListEvent: function() {
            var self = this;
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要删除的班级";
                Alert1.init();
                Alert1.show();
            } else {
                var list = [];
                // var lists = [];
                for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                    // lists.push(Number($(".oneCheck[value=1]").eq(i).attr("data-classid")));
                    list.push($(".oneCheck[value=1]").eq(i).attr("data-classid"));
                }
                var jsons = JSON.stringify(list);
                loading1.show();
                $.ajax({
                    type: "post",
                    url: "getDelClassMark",
                    data: {
                        "jsonStr":jsons
                    },
                    success: function(msg){
                        loading1.hide();
                        msg = JSON.parse(msg);
                        if(msg.result == "success"){
                            if(msg.relationMark == 1){
                                confirm1.content = "<span title="+ msg.classNames +" style='text-align: right;width: 200px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;display:none;float: left;'>"+msg.classNames+"</span><span style='display: inline-block;float: left;'>班级有任课信息，确认删除吗?</span>";

                            }else if(msg.relationMark == 2){
                                confirm1.content = "<span title="+ msg.classNames +" style='text-align: right;width: 200px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;display:none;float: left;'>"+msg.classNames+"</span><span style='display: inline-block;float: left;'>删除班级中有学生，确认删除吗?</span>";

                            }else if(msg.relationMark == 3){
                                confirm1.content = "<span title="+ msg.classNames +" style='text-align: right;width: 200px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;display:none;float: left;'>"+msg.classNames+"</span><span style='display: inline-block;float: left;'>删除班级中有任课信息及学生，确认删除吗?</span>";

                            }else{
                                confirm1.content = "确定删除这<a style='color:#c82011;text-decoration:underline;font-weight:bolder;' href='javascript:;'>"+$(".oneCheck[value=1]").length+"</a>个班级吗";
                            }
                            confirm1.success = function() {
                                var  jsons={"list":list,"type":1};
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "isVm",
                                    data: {
                                        "jsonStr": JSON.stringify(jsons)
                                    },
                                    success: function (msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        if(msg.result == "success"){
                                            if(msg.isVmtype == 1){
                                                Alert1.content = msg.msg;
                                                Alert1.success = function () {
                                                    $(".popups,.masking").show();
                                                    $(".outVirBtn").off("click").click(function() {
                                                        var jsonStr = JSON.stringify({"list":list,"marks":1});
                                                        loading1.show();
                                                        $.ajax({
                                                            type: "post",
                                                            url: "delClass",
                                                            data: {
                                                                "jsonStr":jsonStr
                                                            },
                                                            success: function(msg){
                                                                loading1.hide();
                                                                msg = JSON.parse(msg);
                                                                if(msg.result == "success"){
                                                                    self.tabelList(self.some);
                                                                }else{
                                                                    if(msg.errorMsg != "" && msg.errorMsg != undefined){
                                                                        Alert1.content = msg.errorMsg;
                                                                    }else{
                                                                        Alert1.content = "删除失败";
                                                                    }
                                                                    Alert1.init();
                                                                    Alert1.show();
                                                                }

                                                            },
                                                            error:function () {
                                                                var json = JSON.stringify({
                                                                    "pageName": "error"
                                                                });
                                                                window.location.href = "skipPage?jsonStr=" + json;
                                                            }

                                                        });
                                                    });
                                                    $(".delVirBtn").off("click").click(function() {
                                                        var jsonStr = JSON.stringify({"list":list,"marks":2});
                                                        loading1.show();
                                                        $.ajax({
                                                            type: "post",
                                                            url: "delClass",
                                                            data: {
                                                                "jsonStr":jsonStr
                                                            },
                                                            success: function(msg){
                                                                loading1.hide();
                                                                msg = JSON.parse(msg);
                                                                if(msg.result == "success"){
                                                                    self.tabelList(self.some);
                                                                }else{
                                                                    if(msg.errorMsg != "" && msg.errorMsg != undefined){
                                                                        Alert1.content = msg.errorMsg;
                                                                    }else{
                                                                        Alert1.content = "删除失败";
                                                                    }
                                                                    Alert1.init();
                                                                    Alert1.show();
                                                                }
                                                            },
                                                            error:function () {
                                                                var json = JSON.stringify({
                                                                    "pageName": "error"
                                                                });
                                                                window.location.href = "skipPage?jsonStr=" + json;
                                                            }

                                                        });
                                                    });
                                                };
                                                Alert1.init();
                                                Alert1.show();
                                            }else{
                                                var jsonStr = JSON.stringify({"list":list,"marks":1});
                                                loading1.show();
                                                $.ajax({
                                                    type: "post",
                                                    url: "delClass",
                                                    data: {
                                                        "jsonStr":jsonStr
                                                    },
                                                    success: function(msg){
                                                        loading1.hide();
                                                        msg = JSON.parse(msg);
                                                        if(msg.result == "success"){
                                                            var json = JSON.stringify({
                                                                "pageName":"adminClassMana"
                                                            });
                                                            window.location.href = "skipPage?jsonStr="+json;
                                                        }else{
                                                            if(msg.errorMsg != "" && msg.errorMsg != undefined){
                                                                Alert1.content = msg.errorMsg;
                                                            }else{
                                                                Alert1.content = "删除失败";
                                                            }
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }

                                                    }

                                                });
                                            }

                                        }else{
                                            Alert1.content = msg.msg;
                                            Alert1.init();
                                            Alert1.show();
                                        }
                                    }
                                });
                            };
                            confirm1.init();
                            confirm1.show();
                            $(".alert_content").css({"padding": "50px"});
                        }else{
                            Alert1.content = msg.failMsg;
                            Alert1.init();
                            Alert1.show();
                        }
                    },
                    error:function () {
                        var json = JSON.stringify({
                            "pageName": "error"
                        });
                        window.location.href = "skipPage?jsonStr=" + json;
                    }
                });



            }
        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }
    };
    classMana.init();
});